﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using YJKStairs.CommonUnit;
using YJKStairs.DataInfos;

namespace YJKStairs.Kit
{
	internal class ArcSingleRunStairsKit : ProjectionDrawingKit
	{
		public ArcSingleRunStairsKit(Autodesk.Revit.ApplicationServices.Application app) : base(app)
		{
		}

		public bool ArcSingleRunStairsProfileCurves(ref ArcSingleRunStairs param_stairs, bool bDrawNoteInfo, int nBasePointIndex, double dRotateAngle, bool bBottom, bool bTop)
		{
			bool result = true;
			ArcRunData runData = param_stairs.m_RunData;
			double num = runData.RunIncludeAngle;
			if (runData.Clockwise)
			{
				num = -num;
			}
			double num2 = runData.RunRadius - runData.RunWidth;
			double runRadius = runData.RunRadius;
			double angle = runData.RunStartAngle + num;
			double angle2 = runData.RunStartAngle + num / 2.0;
			XYZ runCenter = runData.RunCenter;
			XYZ xyz = Geometry.RotateTo(XYZ.BasisX, runData.RunStartAngle, XYZ.BasisZ);
			XYZ xyz2 = Geometry.RotateTo(XYZ.BasisX, angle, XYZ.BasisZ);
			XYZ xyz3 = Geometry.RotateTo(XYZ.BasisX, angle2, XYZ.BasisZ);
			XYZ xyz4 = runCenter + xyz * num2;
			XYZ xyz5 = runCenter + xyz * runRadius;
			XYZ xyz6 = runCenter + xyz2 * num2;
			XYZ xyz7 = runCenter + xyz2 * runRadius;
			XYZ ptRunMiddleLeft = runCenter + xyz3 * num2;
			XYZ ptRunMiddleRight = runCenter + xyz3 * runRadius;
			List<XYZ> list = new List<XYZ>();
			param_stairs.m_RunData.PtRunBottomLeft = xyz4;
			param_stairs.m_RunData.PtRunBottomRight = xyz5;
			param_stairs.m_RunData.PtRunTopLeft = xyz6;
			param_stairs.m_RunData.PtRunTopRight = xyz7;
			param_stairs.m_RunData.ptRunMiddleLeft = ptRunMiddleLeft;
			param_stairs.m_RunData.ptRunMiddleRight = ptRunMiddleRight;
			param_stairs.m_listPointsSupport.Add(xyz5);
			if (1E-09 >= Math.Abs(num - Math.PI*2))
			{
				list.Add(xyz4);
				list.Add(xyz5);
			}
			else
			{
				list.Add(xyz4);
				list.Add(xyz5);
				list.Add(xyz7);
				list.Add(xyz6);
			}
			XYZ xyz8 = list[nBasePointIndex];
			base.CalulateTransform(param_stairs.m_bLRMirror, param_stairs.m_bTBMirror, dRotateAngle, 0.0, 0.0, xyz8);
			param_stairs.m_TransVector = Geometry.TransformPoint(xyz8, this.m_Transform);
			param_stairs.m_listOtherCurve.AddRange(base.DrawBasePoint(xyz8, 100.0));
			param_stairs.m_Transform = this.m_Transform;
			this.DrawArcSingleRunStairsRunCurves(ref param_stairs, bDrawNoteInfo);
			this.DrawArcSingleRunStairsBeamCurves(ref param_stairs, bBottom, bTop);
			this.DrawArcSingleRunStairsNoteInfo(ref param_stairs);
			if (bDrawNoteInfo)
			{
				this.DrawUpStairsNoteInfo(ref param_stairs);
			}
			return result;
		}

		private void DrawArcSingleRunStairsRunCurves(ref ArcSingleRunStairs param_stairs, bool bDrawNoteInfo)
		{
			if (param_stairs != null)
			{
				ArcRunData runData = param_stairs.m_RunData;
				SingleRunCurveInfo singleRunCurveInfo = new SingleRunCurveInfo();
				singleRunCurveInfo.BottomElevation = runData.BottomElevation;
				base.DrawArcRun(runData, ref singleRunCurveInfo, ref param_stairs.m_listOtherCurve, bDrawNoteInfo);
				param_stairs.m_listRunCurve.Add(singleRunCurveInfo);
			}
		}

		private void DrawArcSingleRunStairsBeamCurves(ref ArcSingleRunStairs param_stairs, bool bBottom, bool bTop)
		{
			if (param_stairs != null)
			{
				ArcRunData runData = param_stairs.m_RunData;
				double bottomElevation = runData.BottomElevation;
				double beamWidth = param_stairs.m_BeamData.BeamWidth;
				XYZ ptRunBottomLeft = runData.PtRunBottomLeft;
				XYZ ptRunBottomRight = runData.PtRunBottomRight;
				XYZ ptRunTopLeft = runData.PtRunTopLeft;
				XYZ ptRunTopRight = runData.PtRunTopRight;
				if (bBottom)
				{
					List<CurveInfo> list = new List<CurveInfo>();
					XYZ xyz = (ptRunBottomRight - ptRunBottomLeft).Normalize() * beamWidth / 2.0;
					xyz = Geometry.RotateTo(xyz, Math.PI*.5, XYZ.BasisZ);
					if (runData.Clockwise)
					{
						xyz = -xyz;
					}
					XYZ ptS = ptRunBottomLeft - xyz;
					XYZ ptE = ptRunBottomRight - xyz;
					list.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS, ptE, xyz));
					BeamCurveInfo beamCurveInfo = new BeamCurveInfo(list, bottomElevation);
					beamCurveInfo.m_Type = BeamType.BT_Bottom;
					beamCurveInfo.m_nFloorNum = 0;
					param_stairs.m_listBeamCurve.Add(beamCurveInfo);
				}
				if (bTop)
				{
					List<CurveInfo> list2 = new List<CurveInfo>();
					XYZ xyz2 = (ptRunTopRight - ptRunTopLeft).Normalize() * beamWidth / 2.0;
					xyz2 = Geometry.RotateTo(xyz2, Math.PI*.5, XYZ.BasisZ);
					if (runData.Clockwise)
					{
						xyz2 = -xyz2;
					}
					XYZ ptS2 = ptRunTopLeft + xyz2;
					XYZ ptE2 = ptRunTopRight + xyz2;
					list2.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS2, ptE2, xyz2));
					BeamCurveInfo beamCurveInfo2 = new BeamCurveInfo(list2, bottomElevation);
					beamCurveInfo2.m_Type = BeamType.BT_Top;
					beamCurveInfo2.m_nFloorNum = 0;
					param_stairs.m_listBeamCurve.Add(beamCurveInfo2);
				}
			}
		}

		private void DrawArcSingleRunStairsNoteInfo(ref ArcSingleRunStairs param_stairs)
		{
			ArcRunData runData = param_stairs.m_RunData;
			XYZ xyz = XYZ.BasisY * 2.0;
			XYZ xyz2 = XYZ.Zero;
			XYZ ptEnd = XYZ.Zero;
			if (runData.Clockwise)
			{
				xyz2 = runData.PtRunBottomLeft + xyz;
				ptEnd = runData.PtRunBottomRight + xyz;
				base.DrawStairsNoteInfo(ref param_stairs.m_listOtherCurve, ref param_stairs.m_listText, xyz2, ptEnd, "梯段宽度", 0.8, 5.0, 2.0, XYZ.BasisY, PenColor.PC_Black);
			}
			else
			{
				xyz2 = runData.PtRunBottomLeft - xyz;
				ptEnd = runData.PtRunBottomRight - xyz;
				base.DrawStairsNoteInfo(ref param_stairs.m_listOtherCurve, ref param_stairs.m_listText, xyz2, ptEnd, "梯段宽度", 0.8, 5.0, 2.0, -XYZ.BasisY, PenColor.PC_Black);
			}
			double num = runData.RunIncludeAngle;
			if (runData.Clockwise)
			{
				num = -num;
			}
			double angle = runData.RunStartAngle + num / 2.0;
			double angle2 = runData.RunStartAngle + num;
			XYZ xyz3;
			if (Geometry.LessThan(Math.Abs(num), Math.PI*2))
			{
				xyz3 = Geometry.RotateTo(XYZ.BasisX, angle, XYZ.BasisZ);
			}
			else
			{
				double num2 = Geometry.formatAngle(runData.RunStartAngle);
				double dEAngle = Geometry.formatAngle(angle2);
				double num3 = Geometry.BetweenTheAngles(num2, dEAngle, true);
				xyz3 = Geometry.RotateTo(XYZ.BasisX, num2 + num3 / 2.0, XYZ.BasisZ);
				if (Geometry.LessThan(num3, Math.PI))
				{
					xyz3 = Geometry.RotateTo(xyz3, Math.PI, XYZ.BasisZ);
				}
			}
			xyz2 = runData.RunCenter;
			xyz3 *= runData.RunRadius;
			ptEnd = xyz2 + xyz3;
			base.DrawStairsNoteInfo(ref param_stairs.m_listOtherCurve, ref param_stairs.m_listText, xyz2, ptEnd, "R", 0.8, 2.0, 2.0, XYZ.BasisY, PenColor.PC_Black);
		}

		private void DrawUpStairsNoteInfo(ref ArcSingleRunStairs param_stairs)
		{
			ArcRunData runData = param_stairs.m_RunData;
			XYZ xyz = XYZ.BasisY * 1.0;
			List<UpDownText> list = new List<UpDownText>();
			string sText = "上";
			XYZ pos;
			if (runData.Clockwise)
			{
				pos = Geometry.CalculatMidPoint(runData.PtRunBottomLeft, runData.PtRunBottomRight) + xyz;
			}
			else
			{
				pos = Geometry.CalculatMidPoint(runData.PtRunBottomLeft, runData.PtRunBottomRight) - xyz;
			}
			UpDownText item = new UpDownText(pos, sText);
			list.Add(item);
			base.DrawUpDownNoteInfo(ref param_stairs.m_listText, list);
		}
	}
}
